package trading.algo.APIConnectivityTask;

import java.util.Timer;
import java.util.TimerTask;

import trading.algo.TimerKillerTask;
import trading.bo.IBAccessorBO;
import trading.util.IBMsgIDGen;

public class HistoricalDataFarmConnectionTask extends TimerTask {

    private IBAccessorBO ibAccessorBO;
    
    public HistoricalDataFarmConnectionTask(IBAccessorBO ibAccessorBO) {
        this.ibAccessorBO = ibAccessorBO;
    }
    
    @Override
    public void run() {
        
        this.ibAccessorBO.sendHistoricalDataRequest("AMZN");

        IBMsgIDGen ibMsgIDGen = this.ibAccessorBO.getIbMsgIDGen();
        int historicalTaskID = ibMsgIDGen.getCurrentMsgBean().getId();
        
        CancelHistoricalDataTask cancelHistoricalDataTask = new CancelHistoricalDataTask(historicalTaskID, ibAccessorBO);

        Timer timer = new Timer();
        int taskID = 10;
        timer.schedule(cancelHistoricalDataTask, taskID * 1000);
        taskID += CancelHistoricalDataTask.secondsNeeded();
        
        TimerKillerTask timerKillerTask = new TimerKillerTask(timer);
        timer.schedule(timerKillerTask, taskID * 1000);
    }
    
    public static int secondsNeeded() {
        return 1 + 10 + CancelHistoricalDataTask.secondsNeeded();
    }

}
